<?php
/**
 * Application level Controller
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * PHP versions 4 and 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       cake
 * @subpackage    cake.cake.libs.controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

/**
 * This is a placeholder class.
 * Create the same file in app/app_controller.php
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package       cake
 * @subpackage    cake.cake.libs.controller
 * @link http://book.cakephp.org/view/957/The-App-Controller
 */
class AppController extends Controller {
	var $components = array('Auth','Cookie', 'Email', 'Session');
    var $helpers = array('Html' , 'Form', 'Session','Javascript','Ajax', 'Time', 'Text', 'Tinymce', 'FckEditor');
	
	function beforeFilter(){
		$this->Auth->allow('*');
		//$this->Auth->authorize = 'actions';
		$this->Auth->authError = 'Please login to view this page';
		$this->Auth->loginError = 'Invalid username or password';
		$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
		$this->Auth->loginRedirect = array('controller' => 'posts', 'action' => 'index');
		$this->Auth->logoutRedirect = array('controller' => 'posts', 'action' => 'index');
		
		//if admin url requested
		Configure::load('config'); 
		$admin = Configure::read('Routing.admin');
		if (isset($this->params['admin']) && $this->params['admin']) {
			//check logged user is admin
			if ($this->_isAdmin()) {
				//change layout
				//$this->layout = 'admin';  					;
			} else {
				$this->Session->setFlash(__("LOGIN AS ADMIN TO VIEW THIS PAGE", true));
				$this->redirect("/login");
			}
		}
		//$this->Auth->allow('index', 'view');
		$this->set('isAdmin', $this->_isAdmin());
		$this->set('isRepresent', $this->_isRepresent());
		$this->set('logged_in', $this->_loggedIn());
		$this->set('users_username', $this->_usersUsername());
	}
	
	function _isAdmin(){
		$admin = FALSE;
		$this->loadModel('Group');
		$this->Group->id = $this->Auth->user('group_id');
		$role = $this->Group->field('role');
		if($role == 'administrators'){
			$admin = TRUE;
		}
		return $admin;
	}
	
	function _isRepresent(){
		$represent = FALSE;
		$this->loadModel('Group');
		$this->Group->id = $this->Auth->user('group_id');
		$role = $this->Group->field('role');
		if($role == 'represents'){
			$represent = TRUE;
		}
		return $represent;
	}
	
	//callback function
	function _loggedIn(){
		$logged_in = FALSE;
		if($this->Auth->user()){
			$logged_in = TRUE;
		}
		return $logged_in;
	}
	
	function _usersUsername(){
		$users_username = NULL;
		if($this->Auth->user()){
			$users_username['username'] = $this->Auth->user('username');
			$users_username['id'] = $this->Auth->user('id');
			$users_username['group_id'] = $this->Auth->user('group_id');
			$users_username['name'] = $this->Auth->user('name');
		}
		return $users_username;
	}
}
